package org.example;
import java.sql.*;
import java.util.Scanner;


    public class EmployeeManagementSystem {

        private static final String URL = "jdbc:mysql://localhost:3306/employee_management1";
        private static final String USER = "root"; // 数据库用户名
        private static final String PASSWORD = "123456"; // 数据库密码
        private static Connection connection = null;
        private static Scanner scanner = new Scanner(System.in);

        public static void main(String[] args) {
            try {
                // 连接数据库
                connection = DriverManager.getConnection(URL, USER, PASSWORD);
                System.out.println("数据库连接成功！");

                while (true) {
                    System.out.println("请选择操作：");
                    System.out.println("1. 根据ID查询员工信息");
                    System.out.println("2. 新增员工信息");
                    System.out.println("3. 根据ID修改员工信息");
                    System.out.println("4. 根据ID删除员工信息");
                    System.out.println("5. 退出系统");

                    int choice = scanner.nextInt();

                    switch (choice) {
                        case 1:
                            queryEmployeeById();
                            break;
                        case 2:
                            addEmployee();
                            break;
                        case 3:
                            updateEmployeeById();
                            break;
                        case 4:
                            deleteEmployeeById();
                            break;
                        case 5:
                            System.out.println("退出系统。");
                            return;
                        default:
                            System.out.println("无效选择，请重新选择。");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (connection != null && !connection.isClosed()) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        // 根据ID查询员工信息
        public static void queryEmployeeById() {
            System.out.println("请输入要查询的员工ID：");
            int id = scanner.nextInt();
            String query = "SELECT * FROM employees WHERE id = ?";
            try (PreparedStatement stmt = connection.prepareStatement(query)) {
                stmt.setInt(1, id);
                ResultSet rs = stmt.executeQuery();
                if (rs.next()) {
                    System.out.println("ID: " + rs.getInt("id"));
                    System.out.println("姓名: " + rs.getString("name"));
                    System.out.println("年龄: " + rs.getInt("age"));
                    System.out.println("部门: " + rs.getString("department"));
                } else {
                    System.out.println("该员工不存在。");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        // 新增员工信息
        public static void addEmployee() {
            System.out.println("请输入员工姓名：");
            scanner.nextLine(); // 消除换行符
            String name = scanner.nextLine();
            System.out.println("请输入员工年龄：");
            int age = scanner.nextInt();
            System.out.println("请输入员工部门：");
            scanner.nextLine(); // 消除换行符
            String department = scanner.nextLine();

            String insert = "INSERT INTO employees (name, age, department) VALUES (?, ?, ?)";
            try (PreparedStatement stmt = connection.prepareStatement(insert)) {
                stmt.setString(1, name);
                stmt.setInt(2, age);
                stmt.setString(3, department);
                int rowsAffected = stmt.executeUpdate();
                if (rowsAffected > 0) {
                    System.out.println("员工信息添加成功！");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        // 根据ID修改员工信息
        public static void updateEmployeeById() {
            System.out.println("请输入要修改的员工ID：");
            int id = scanner.nextInt();
            scanner.nextLine(); // 消除换行符
            System.out.println("请输入新的姓名：");
            String name = scanner.nextLine();
            System.out.println("请输入新的年龄：");
            int age = scanner.nextInt();
            System.out.println("请输入新的部门：");
            scanner.nextLine(); // 消除换行符
            String department = scanner.nextLine();

            String update = "UPDATE employees SET name = ?, age = ?, department = ? WHERE id = ?";
            try (PreparedStatement stmt = connection.prepareStatement(update)) {
                stmt.setString(1, name);
                stmt.setInt(2, age);
                stmt.setString(3, department);
                stmt.setInt(4, id);

                int rowsAffected = stmt.executeUpdate();
                if (rowsAffected > 0) {
                    System.out.println("员工信息修改成功！");
                } else {
                    System.out.println("修改失败，该员工不存在。");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        // 根据ID删除员工信息
        public static void deleteEmployeeById() {
            System.out.println("请输入要删除的员工ID：");
            int id = scanner.nextInt();
            String delete = "DELETE FROM employees WHERE id = ?";
            try (PreparedStatement stmt = connection.prepareStatement(delete)) {
                stmt.setInt(1, id);
                int rowsAffected = stmt.executeUpdate();
                if (rowsAffected > 0) {
                    System.out.println("员工信息删除成功！");
                } else {
                    System.out.println("删除失败，该员工不存在。");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

